<template>
	<view class="login-container">
		<view class="login-header">
			<image class="logo" src="/static/logo小.jpg" mode="aspectFit" />
			<text class="title">欢迎登录搭膳</text>
		</view>

		<view class="login-form">
			<view class="form-item">
				<text class="label">用户名</text>
				<input class="input" type="text" placeholder="请输入用户名" v-model="username" @blur="validateUsername" />
				<text v-if="usernameError" class="error-text">{{ usernameError }}</text>
			</view>

			<view class="form-item">
				<text class="label">密码</text>
				<input class="input" type="password" placeholder="请输入密码" v-model="password" @blur="validatePassword" />
				<text v-if="passwordError" class="error-text">{{ passwordError }}</text>
			</view>

			<view class="form-item checkbox-item">
				<label class="checkbox-label">
					<checkbox :checked="rememberLogin" @tap="toggleRememberLogin" />
					<text class="checkbox-text">7天免登录</text>
				</label>
			</view>

			<view class="notice">
				<text class="notice-title">注意事项：</text>
				<text class="notice-text">
					1. 本应用提供游客账号：
					<br />
					账号名：游客、密码：yk3604】
				</text>
				<text class="notice-text">2. 本应用为个人应用，仅限部分人员使用</text>
				<text class="notice-text">3. 如发现异常请及时联系作者</text>
			</view>

			<button class="login-button" @tap="handleLogin">登录</button>

			<view class="footer">
				<text>还没有账号？</text>
				<text class="link" @tap="goToRegister">立即申请</text>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			username: '',
			password: '',
			rememberLogin: false,
			usernameError: '',
			passwordError: ''
		};
	},
	methods: {
		validateUsername() {
			if (!this.username) {
				this.usernameError = '请输入用户名';
				return false;
			} else {
				this.usernameError = '';
				return true;
			}
		},
		validatePassword() {
			if (!this.password) {
				this.passwordError = '请输入密码';
				return false;
			} else if (this.password.length < 6) {
				this.passwordError = '密码长度不能少于6位';
				return false;
			} else {
				this.passwordError = '';
				return true;
			}
		},
		toggleRememberLogin() {
			this.rememberLogin = !this.rememberLogin;
		},
		handleLogin() {
			// 验证表单
			const isUsernameValid = this.validateUsername();
			const isPasswordValid = this.validatePassword();
			const isLogin = this.rememberLogin;
		
			if (isUsernameValid && isPasswordValid) {
				uniCloud
					.callFunction({
						name: 'UserList',
						data: {
							action: 'findUser',
							params: {
								userName: this.username,
								passWord: this.password
							}
						}
					})
					.then(res => {
						const { data } = res?.result || {};
						console.log('res', data);
						if (data && data.length > 0) {
							const { userName, userId, role, correlation } = data[0] || {};
							const now = new Date().getTime(); // 获取当前时间的时间戳
							const token = {
								userName,
								userId,
								role,
								isLogin: isLogin,
								time: now,
								correlation
							};
							uni.setStorageSync('token', token);
							uni.showToast({
								title: '登录成功',
								icon: 'success'
							});
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/home/home'
								});
							}, 1000);
						} else {
							uni.showToast({
								title: `用户名或密码错误`,
								icon: 'error'
							});
						}
						this.loadingType = false;
					});
			} else {
				uni.showToast({
					title: '请填写正确的信息',
					icon: 'none'
				});
			}
		},
		goToRegister() {
			// 跳转到注册页面
			uni.navigateTo({
				url: '/pages/apply/apply'
			});
		}
	}
};
</script>

<style scoped>
.login-container {
	box-sizing: border-box;
	padding: 20px;
	background-color: #f5f5f5;
	height: 100vh;
	background: #1c92d2;
	background: -webkit-linear-gradient(to top, #1c92d2, #f2fcfe);
	background: linear-gradient(to top, #1c92d2, #f2fcfe);
	overflow: hidden;
}

.login-header {
	display: flex;
	flex-direction: column;
	justify-content: center;
	text-align: center;
	margin-bottom: 60rpx;
	padding-top: 100rpx;
}

.logo {
	width: 120rpx;
	height: 120rpx;
	margin: 0 auto 20rpx;
	overflow: hidden;
	border-radius: 4px;
}

.title {
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
}

.login-form {
	background-color: #fff;
	border-radius: 10rpx;
	padding: 40rpx;
	box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
}

.form-item {
	margin-bottom: 30rpx;
}

.label {
	display: block;
	font-size: 28rpx;
	color: #333;
	margin-bottom: 15rpx;
}

.input {
	border: 1rpx solid #ddd;
	border-radius: 8rpx;
	padding: 20rpx;
	font-size: 28rpx;
	height: 50rpx;
}

.checkbox-item {
	display: flex;
	align-items: center;
}

.checkbox-label {
	display: flex;
	align-items: center;
}

.checkbox-text {
	margin-left: 15rpx;
	font-size: 26rpx;
	color: #666;
}

.notice {
	background-color: #f0f8ff;
	border-radius: 8rpx;
	padding: 20rpx;
	margin: 30rpx 0;
}

.notice-title {
	font-weight: bold;
	color: #333;
	display: block;
	margin-bottom: 10rpx;
}

.notice-text {
	display: block;
	font-size: 24rpx;
	color: #666;
	line-height: 1.5;
}

.login-button {
	background-color: #1e88e5;
	color: white;
	border: none;
	border-radius: 8rpx;
	padding: 20rpx;
	font-size: 32rpx;
	width: 100%;
	margin-top: 20rpx;
}

.login-button:active {
	background-color: #1976d2;
}

.footer {
	text-align: center;
	margin-top: 40rpx;
	font-size: 26rpx;
	color: #666;
}

.link {
	color: #1e88e5;
	margin-left: 10rpx;
}

.error-text {
	color: #e53935;
	font-size: 24rpx;
	margin-top: 10rpx;
	display: block;
}
</style>
